from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin,RetrieveModelMixin
from . import models,serializers

# 课程分类群查
class CourseCategoryViewSet(GenericViewSet,ListModelMixin):
    queryset = models.CourseCategory.objects.filter(is_delete=False,is_show=True).all()
    serializer_class = serializers.CourseCategorySerializer

# 课程群查
# 分页组件，基础分页（采用），偏移分页，游标分页（了解）
from rest_framework.pagination import PageNumberPagination,LimitOffsetPagination,CursorPagination
from . import pagination

# 过滤组件：搜索功能，排序功能
from rest_framework.filters import SearchFilter,OrderingFilter
from .filters import CountFilter

# django-filter插件 分类功能
from django_filters.rest_framework import DjangoFilterBackend
from .filters import CourseFilterSet
class CourseViewSet(GenericViewSet,ListModelMixin,RetrieveModelMixin):
    queryset = models.Course.objects.filter(is_delete=False,is_show=True).all()
    serializer_class = serializers.CourseSerializer

    # 分页组件
    # 方法一：直接使用drf分页类，在视图类中完成分页类的必要配置
    # pagination_class = PageNumberPagination
    # PageNumberPagination.page_size = 2

    # 方法二：自定义分页类继承drf分页类，在自定义分页类中完成配置，视图类中使用自定义分页类
    pagination_class = pagination.PageNumberPagination  # 基础分页器
    # pagination_class = pagination.LimitOffsetPagination   # 偏移分页器
    # pagination_class = pagination.CursorPagination         # 游标分页器

    # 过滤组件：实际开发，有多个过滤条件时，要把优先级高的放在前面
    filter_backends = [SearchFilter,OrderingFilter,DjangoFilterBackend,CountFilter]

    # 参与搜索的字段
    search_fields = ['name', 'id','brief']

    # 分类的字段(分组的字段)
    # filter_fields = ['course_category']

    # 过滤类
    filter_class = CourseFilterSet

    # 允许排序的字段
    ordering_fields = ['id', 'price','students']
"""
GenericAPIView:额外的两个属性两个方法
filter_backends: 配置过滤类们
filter_queryset(): 调用配置的过滤类完成过滤（都是在群查接口中完成，传入queryset,接受过滤后的queryset）

pagination_class: 配置分页类
paginate_queryset(): 调用配置的分页类完成分页（都是在群查接口中完成，传入queryset,接受过滤后的queryset）
"""

# 查一个课程所有章节的接口（群查）
class ChapterViewSet(GenericViewSet,ListModelMixin):
    queryset = models.CourseChapter.objects.filter(is_delete=False,is_show=True).all()
    serializer_class = serializers.CourseChapterSerializer

    filter_backends = [DjangoFilterBackend]
    filter_fields = ['course']


# 搜索课程接口
class SearchCourseViewSet(GenericViewSet,ListModelMixin):
    queryset = models.Course.objects.filter(is_delete=False,is_show=True).all()
    serializer_class = serializers.CourseSerializer
    pagination_class = pagination.PageNumberPagination
    filter_backends = [SearchFilter]
    search_fields = ['name']